package com.spzx.cart.controller;

import com.spzx.cart.domain.CartInfo;
import com.spzx.cart.service.ICartService;
import com.spzx.common.core.domain.R;
import com.spzx.common.core.web.controller.BaseController;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.security.annotation.RequiresLogin;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "购物车接口")
@RestController
@RequestMapping
public class CartController extends BaseController {

    @Autowired
    private ICartService cartService;

    //添加商品到购物车
    @RequiresLogin
    @GetMapping("/addToCart/{skuId}/{skuNum}")
    public AjaxResult addToCart(@PathVariable(value = "skuId") Long skuId,
                                @PathVariable("skuNum") Integer skuNum) {
        cartService.getAddToCart(skuId, skuNum);
        return success();
    }

    //显示购物车列表
//    @RequiresLogin
    @Operation(summary = "查询购物车")
    @RequiresLogin
    @GetMapping("/cartList")
    public AjaxResult getCartList() {
        List<CartInfo> cartInfoList = cartService.getCartList();
        return success(cartInfoList);
    }

    @Operation(summary = "删除购物车商品")
    @RequiresLogin
    @DeleteMapping ("/deleteCart/{skuId}")
    public AjaxResult deleteCart(@PathVariable("skuId") Long skuId) {
        cartService.deleteCart(skuId);
        return success();
    }

    @Operation(summary = "更新选中状态")
    @RequiresLogin
    @GetMapping("/checkCart/{skuId}/{isChecked}")
    public AjaxResult checkCart(@Parameter(name = "skuId", description = "商品skuId", required = true)
                                @PathVariable(value = "skuId") Long skuId,
                                @Parameter(name = "isChecked", description = "是否选中  1：选中  0:取消选中", required = true)
                                @PathVariable(value = "isChecked") Integer isChecked) {
        cartService.checkCart(skuId, isChecked);
        return success();

    }

    //全选全不选
    @Operation(summary = "全选全不选")
    @RequiresLogin
    @GetMapping("/allCheckCart/{isChecked}")
    public AjaxResult allCart(@Parameter(name = "isChecked",description = "是否选中  1：选中  0：取消",required = true)
            @PathVariable(value = "isChecked") Integer isChecked){

        cartService.getAllCart(isChecked);
        return success();
    }
    @Operation(summary = "清空购物车")
    @RequiresLogin
    @GetMapping("/clearCart")
    public AjaxResult deleteCart(){
        cartService.deleteAllCart();
        return success();
    }

    //远程调用接口，获取购物车选中的商品列表
    @Operation(summary = "获取购物车选中的商品列表")
    @RequiresLogin
    @GetMapping("/getCartCheckedList/{userId}")
    public AjaxResult getCartCheckedList(@PathVariable(value = "userId") Long userId)
    {
       List<CartInfo>  cartInfoList = cartService.getCartCheckedList();
       return success(cartInfoList);
    }
}
